let exp = module.exports = {};

exp.setKeepScreenOn = function (isOn) {
    if (cc.sys.isNative){
        if (cc.sys.os === cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "setKeepScreenOn", "(Z)V",isOn);
        }else if (cc.sys.os === cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod("AppController", "setKeepScreenOn:", isOn);
        }
    }
};

exp.copyText = function (text) {
    if (cc.sys.isNative){
        if (cc.sys.os === cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "copyText", "(Ljava/lang/String;)V", text);
        }else if (cc.sys.os === cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod("AppController", "copyText:", text);
        }
    }else{
        Global.DialogManager.addTipDialog("当前平台不支持");
    }
};

exp.openAppByUrl = function (url) {
    if (cc.sys.isNative){
        if (cc.sys.os === cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "openAppByUrl", "(Ljava/lang/String;)V", url);
        }else if (cc.sys.os === cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod("AppController", "openAppByUrl:", url);
        }
    }
};

let GET_LOCATION_CALLBACK = null;
exp.getLocation = function (cb) {
    GET_LOCATION_CALLBACK = cb;
    if (cc.sys.isNative){
        if (cc.sys.platform === cc.sys.ANDROID) {
            return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "getLocation", "()V")
        } else if (cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD) {
            return jsb.reflection.callStaticMethod("AppController", "getLocation")
        }
    }else{
        setTimeout(function () {
            if (!!Global.wxAccountInfo){
                exp.getLocationFinished(0, JSON.stringify({
                    location: Global.wxAccountInfo.address,
                    latitude: Global.Utils.getRandomNum(10000, 99999)/1000,
                    longitude: Global.Utils.getRandomNum(10000, 99999)/1000
                }));
            } else{
                exp.getLocationFinished(0, JSON.stringify({
                    location: "",
                    latitude: Global.Utils.getRandomNum(10000, 99999)/1000,
                    longitude: Global.Utils.getRandomNum(10000, 99999)/1000
                }));
            }
        }, 100);
    }
};

exp.getLocationFinished = function (errCode, result) {
    if (!errCode){
        result = JSON.parse(result);
        Global.Utils.invokeCallback(GET_LOCATION_CALLBACK, null, {
            location: result.location,
            address: JSON.stringify({
                x: result.latitude,
                y: result.longitude
            })
        });
    } else{
        Global.Utils.invokeCallback(GET_LOCATION_CALLBACK, 1);
    }
};

exp.getNetworkDelay = function (cb) {
    if (cc.sys.isNative){
        if (cc.sys.platform === cc.sys.ANDROID) {
            return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "getNetworkDelay", "()V")
        } else if (cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD) {
            return jsb.reflection.callStaticMethod("AppController", "getNetworkDelay")
        }
    }else{
        Global.Utils.invokeCallback(cb, Global.Utils.getRandomNum(30, 500));
    }
};

exp.getBattery = function () {
    if (cc.sys.isNative){
        if (cc.sys.platform === cc.sys.ANDROID) {
            return jsb.reflection.callStaticMethod("org/cocos2dx/javascript/CommonAPI", "getBattery", "()V")
        } else if (cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD) {
            return jsb.reflection.callStaticMethod("AppController", "getBattery")
        }
    }else{
        return 0.3;
    }
};

// -------------------------------------------------微信相关-------------------------------------------------------------
//微信授权登录
exp.wxLogin = function () {
    if (cc.sys.platform === cc.sys.ANDROID) {
        jsb.reflection.callStaticMethod("com.huiyou.huiyouqipai/wxapi/WXSDKAPI", "wxLogin", "()V")
    } else if ((cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD)) {
        jsb.reflection.callStaticMethod("AppController", "wxLogin")
    } else{
        setTimeout(function () {
            exp.wxLoginFinished(0, JSON.stringify({
                openid: Global.Utils.randomString(16),
                nickname: "wx" + Global.Utils.getRandomNum(100000, 999999),
                headimgurl: "Common/head_icon_default",
                sex: Global.Utils.getRandomNum(0, 1)
            }))
        }, 1);
    }
};

//微信授权登录结果
exp.wxLoginFinished = function (errCode, userInfo) {
    console.log('wxLoginFinished');
    if (!errCode){
        console.log("wxLoginFinished", userInfo);
        let wxUserData = JSON.parse(userInfo);

        Global.MessageCallback.emitMessage('PlatformLogin', {platform: Global.Enum.loginPlatform.WEI_XIN, userInfo: wxUserData});
    }else{
        console.log('wxLoginFinished errCode:', errCode);
    }
};

// 微信分享
// sceneType: 0 朋友， 1朋友圈
exp.wxShareImg = function (imgPath, sceneType) {
    if (cc.sys.platform === cc.sys.ANDROID) {
        jsb.reflection.callStaticMethod("com.huiyou.huiyouqipai/wxapi/WXSDKAPI", "shareImageToWX", "(Ljava/lang/String;I)V", imgPath, sceneType);
    } else if (cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD) {
        jsb.reflection.callStaticMethod("AppController", "imgPath:sceneType:", imgPath, sceneType);
    } else {
        Global.DialogManager.addTipDialog("当前平台不支持");
    }
};

exp.wxShareUrl = function (url, imgPath, title, description, sceneType) {
    if (cc.sys.platform === cc.sys.ANDROID) {
        jsb.reflection.callStaticMethod("com.huiyou.huiyouqipai/wxapi/WXSDKAPI", "shareUrlToWX", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V", url, imgPath, title, description, sceneType);
    } else if (cc.sys.platform === cc.sys.IPHONE || cc.sys.platform === cc.sys.IPAD) {
        jsb.reflection.callStaticMethod("AppController", "wxShareUrl:imgPath:title:description:sceneType:", url, imgPath, title, description, sceneType);
    } else{
        Global.DialogManager.addTipDialog("当前平台不支持");
    }
};

exp.wxShareFinished = function (errCode) {
    console.log('wxShareFinished');

    Global.MessageCallback.emitMessage('PlatformShare', {platform: Global.Enum.loginPlatform.WEI_XIN, errCode: errCode});
};
